package com.iraeta.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iraeta.api.HikCameraApi;
import com.iraeta.common.Result;
import com.iraeta.entity.TreeNode;
import com.iraeta.entity.basic.HikCamera;
import com.iraeta.entity.basic.type.HikRegionTreeNode;
import com.iraeta.service.basic.IHikCameraService;
import com.iraeta.service.basic.IHikRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController(value = "HikDataController")
@RequestMapping("hikdata")
public class HikDataController extends BaseController {
    @Autowired
    IHikRegionService hikRegionService;
    @Autowired
    IHikCameraService hikCameraService;
    @Autowired
    HikCameraApi hikCameraApi;

    @RequestMapping("gethikregiontablenode")
    public String getHikRegionTableNode(){
        List<HikRegionTreeNode> hikRegionTreeNodeList = hikRegionService.getHikRegionTableNode();
        return this.success(hikRegionTreeNodeList);
    }
    @RequestMapping("updateregion")
    public String updateRegion(){
        try {
            hikRegionService.updateHikRegionFromHikIsc();
            return this.success();
        }catch (Exception e){
            return JSON.toJSONString(Result.error(e.getMessage()));
        }
    }

    @RequestMapping("gethikregiontreenode")
    public String getHikRegionTreeNode(){
        List<TreeNode> hikRegionTreeNodeList = hikRegionService.getHikRegionTreeNode();
        return this.success(hikRegionTreeNodeList);
    }
    @RequestMapping("listregioncamera")
    public String lisrRegionCamera(@RequestBody JSONObject jo){
        String name = jo.getString("searchName");
        String indexcode = jo.getString("activeRegionCode");
        List<HikCamera> cameraList = hikCameraService.listRegionCamera(indexcode, name);
        return this.success(cameraList);
    }
    @RequestMapping("getcameradata")
    public String getCameraData(@RequestBody JSONObject jo){
        int id = jo.getIntValue("id");
        return this.success(hikCameraService.getById(id));
    }
    @RequestMapping("updatecamera")
    public String updateCamera(){
        try {
            hikCameraService.updateHikCameraFromHikIsc();
            return this.success();
        }catch (Exception e){
            return JSON.toJSONString(Result.error(e.getMessage()));
        }
    }
    @RequestMapping("getcameraurl")
    public String getCameraUrl(@RequestBody JSONObject jo) throws Exception{
        int id = jo.getIntValue("id");
        HikCamera hikCamera = hikCameraService.getById(id);
        String url = hikCameraApi.getCameraPreviewHls(hikCamera.getIndexcode());
        return this.success(url);
    }
    @RequestMapping("getcameraurl_ws")
    public String getCameraUrl_ws(@RequestBody JSONObject jo) throws Exception{
        int id = jo.getIntValue("id");
        HikCamera hikCamera = hikCameraService.getById(id);
        String url = hikCameraApi.getCameraPreviewWs(hikCamera.getIndexcode());
        return this.success(url);
    }
}
